package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.zm.cg.domain.ZMCGNotice;

/**
 * 审批通知
 * <p>Title: ServiceZMCG16.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-7-8 上午05:03:26  严洪浩  Create
 *</pre>
 */
public class ServiceZMCG1101 extends ServiceEPBase {

	/**
	 * 初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:44 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:14:50 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		if(inInfo.getBlock(EiConstant.queryBlock) == null){
			StaticTools.setQueryParameters(inInfo, "fdEndDate", StaticTools.getDate() + " 23:59:59", true);
		}
		else {
			String endTime = inInfo.getBlock(EiConstant.queryBlock).getCellStr(0, "fdEndDate");
			if(null != endTime && !"".equals(endTime)) {
				if(endTime.length() <= 10) {
					StaticTools.setQueryParameters(inInfo, "fdEndDate", endTime + " 23:59:59", true);
				}
			}
		}
		
		String fdApprovalId = UserSession.getUserId() == null ? "" : UserSession.getUserId().toString();//编制
		StaticTools.setQueryParameters(inInfo, "fdApprovalId", fdApprovalId, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdStatus", "草拟", true);//不能看到草拟的
		StaticTools.setQueryParameters(inInfo, "fdIsApproval", "1", true);
		
		String fdApproval = UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString();//审批人
		StaticTools.setQueryParameters(inInfo, "fdApproval", fdApproval, true);//添加查询条件
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCGNotice.query2", new ZMCGNotice());
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		return outInfo;
	}
	
	/**
	 * 修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:10 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdStatus", _dom.getFdStatus());
			params.put("fdPerson", _dom.getFdPerson());
			params.put("fdTime", _dom.getFdTime());
			params.put("fdIsTop", _dom.getFdIsTop());
			params.put("fdIsApproval", _dom.getFdIsApproval());
			params.put("fdApproval", _dom.getFdApproval());
			if(validate(_dom)){
			this.dao.update("ZMCGNotice.updateApproval",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能修改待审批或不通过的通知!"); 
				return out;
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * 通过
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo pass(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.update("ZMCGNotice.modifyPass",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能通过待审批或不通过的通知!"); 
				return out;
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
	
	/**
	 * 不通过
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCG15</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-7-8上午05:15:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo nopass(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCGNotice _dom = new ZMCGNotice();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if(validate(_dom)){
			this.dao.delete("ZMCGNotice.modifyNopass",_dom);
			}else{
				out = this.query(inInfo);
				out.setStatus(-1);
				out.setMsg("只能不通过待审批的通知!"); 
				return out;
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	private boolean validate(ZMCGNotice _dom){
		String fdStatus = _dom.getFdStatus();
		if("待审批".equals(fdStatus)||"不通过".equals(fdStatus)){
			return true; 
		}else
			return false;
	}
}		

